import 'package:flutter/material.dart';

class User {
  String name;
  String flag;
  int goldCount;
  int silverCount;
  int copperCount;
  int totalCount;
  bool selected;

  User(this.name, this.flag, this.goldCount, this.silverCount, this.copperCount,
      this.totalCount,
      {this.selected = false});
}

class Olympic12 extends StatefulWidget {
  Olympic12({Key? key}) : super(key: key);

  @override
  _Olympic12State createState() => _Olympic12State();
}

class _Olympic12State extends State<Olympic12> {
  List<User> data = [
    User(
        '中国',
        'https://search-operate.cdn.bcebos.com/7dce3e5758a82e720ec1c7123d246616.png',
        38,
        32,
        18,
        88,
        selected: true),
    // <img src="https://search-operate.cdn.bcebos.com/7dce3e5758a82e720ec1c7123d246616.png">
    User(
        '日本',
        'https://search-operate.cdn.bcebos.com/4d04bde9a3248455c0d977e287c531e1.png',
        27,
        14,
        17,
        58),
    // <img src="https://search-operate.cdn.bcebos.com/4d04bde9a3248455c0d977e287c531e1.png">
    User(
        '美国',
        'https://search-operate.cdn.bcebos.com/19f0d6e267727f9846559d420a2068a6.png',
        39,
        41,
        33,
        113),
    // <img src="https://search-operate.cdn.bcebos.com/19f0d6e267727f9846559d420a2068a6.png">
    User(
        '俄罗斯奥委会',
        'https://search-operate.cdn.bcebos.com/8c00d69be531ba4b21d5100d2a13fb8b.png',
        20,
        28,
        23,
        71),
    // <img src="https://search-operate.cdn.bcebos.com/8c00d69be531ba4b21d5100d2a13fb8b.png">
    User(
        '澳大利亚',
        'https://search-operate.cdn.bcebos.com/9c6ae919870434634765897f52ca26b1.png',
        17,
        7,
        22,
        46),
    // <img src="https://search-operate.cdn.bcebos.com/9c6ae919870434634765897f52ca26b1.png">
    User(
        '英国',
        'https://search-operate.cdn.bcebos.com/c898755909037b5107f517dac2cd3be0.png',
        22,
        21,
        22,
        65),
    // <img src="https://search-operate.cdn.bcebos.com/c898755909037b5107f517dac2cd3be0.png">
  ];

  var _sortAscending = true;
  var _SortColumnIndex = 1;

  List<DataRow> _getUserRows(displayWidth) {
    // requestPermission();
    List<DataRow> dataRows = [];
    for (int i = 0; i < data.length; i++) {
      dataRows.add(DataRow(
          selected: data[i].selected,
          onSelectChanged: (selected) {
            setState(() {
              data[i].selected = selected!;
            });
          },
          cells: [
            DataCell(
              Container(
                  width: displayWidth / 10,
                  child: Text(
                    '${data[i].name}',
                    textAlign: TextAlign.left,
                  )),
            ),
            DataCell(
              Container(
                  width: displayWidth / 15,
                  child: Image.network(
                    '${data[i].flag}',
                  )),
            ),
            DataCell(
              Container(
                  width: displayWidth / 10,
                  child: Text(
                    '${data[i].goldCount}',
                    textAlign: TextAlign.left,
                  )),
            ),
            DataCell(
              Container(
                  width: displayWidth / 10,
                  child: Text(
                    '${data[i].silverCount}',
                    textAlign: TextAlign.left,
                  )),
            ),
            DataCell(
              Container(
                  width: displayWidth / 10,
                  child: Text(
                    '${data[i].copperCount}',
                    textAlign: TextAlign.left,
                  )),
            ),
            DataCell(
              Container(
                  width: displayWidth / 10,
                  child: Text(
                    '${data[i].totalCount}',
                    textAlign: TextAlign.left,
                  )),
            ),
          ]));
    }
    return dataRows;
  }

  @override
  Widget build(BuildContext context) {
    double displayWidth = MediaQuery.of(context).size.width;
    return Container(
      child: SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          child: DataTable(
            sortColumnIndex: _SortColumnIndex,
            sortAscending: _sortAscending,
            dataRowHeight: 100,
            horizontalMargin: 15,
            columnSpacing: 100,
            columns: [
              DataColumn(
                  label: Container(
                      width: displayWidth / 10,
                      child: Text(
                        '国家/地区',
                        textAlign: TextAlign.left,
                      ))),
              DataColumn(
                  label: Container(
                      width: displayWidth / 15,
                      child: Text(
                        '',
                        textAlign: TextAlign.left,
                      ))),
              DataColumn(
                  label: Container(
                    width: displayWidth / 10,
                    child: Text(
                      '金',
                      textAlign: TextAlign.left,
                    ),
                  ),
                  numeric: true,
                  onSort: (int columnIndex, bool asscending) {
                    setState(() {
                      _SortColumnIndex = columnIndex;
                      _sortAscending = asscending;
                      if (asscending) {
                        data.sort((a, b) => a.goldCount.compareTo(b.goldCount));
                      } else {
                        data.sort((a, b) => b.goldCount.compareTo(a.goldCount));
                      }
                    });
                  }),
              DataColumn(
                  label: Container(
                    width: displayWidth / 10,
                    child: Text(
                      '银',
                      textAlign: TextAlign.left,
                    ),
                  ),
                  numeric: true,
                  onSort: (int columnIndex, bool asscending) {
                    setState(() {
                      _SortColumnIndex = columnIndex;
                      _sortAscending = asscending;
                      if (asscending) {
                        data.sort(
                            (a, b) => a.silverCount.compareTo(b.silverCount));
                      } else {
                        data.sort(
                            (a, b) => b.silverCount.compareTo(a.silverCount));
                      }
                    });
                  }),
              DataColumn(
                  label: Container(
                    width: displayWidth / 10,
                    child: Text(
                      '铜',
                      textAlign: TextAlign.left,
                    ),
                  ),
                  numeric: true,
                  onSort: (int columnIndex, bool asscending) {
                    setState(() {
                      _SortColumnIndex = columnIndex;
                      _sortAscending = asscending;
                      if (asscending) {
                        data.sort(
                            (a, b) => a.copperCount.compareTo(b.copperCount));
                      } else {
                        data.sort(
                            (a, b) => b.copperCount.compareTo(a.copperCount));
                      }
                    });
                  }),
              DataColumn(
                  label: Container(
                    width: displayWidth / 10,
                    child: Text(
                      '总数',
                      textAlign: TextAlign.left,
                    ),
                  ),
                  numeric: true,
                  onSort: (int columnIndex, bool asscending) {
                    setState(() {
                      _SortColumnIndex = columnIndex;
                      _sortAscending = asscending;
                      if (asscending) {
                        data.sort(
                            (a, b) => a.totalCount.compareTo(b.totalCount));
                      } else {
                        data.sort(
                            (a, b) => b.totalCount.compareTo(a.totalCount));
                      }
                    });
                  })
            ],
            rows: _getUserRows(displayWidth),
          )),
    );
  }
}